package com.dsa.team5.kanban.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dsa.team5.kanban.model.Kanban;
import com.dsa.team5.kanban.model.Status;
import com.dsa.team5.kanban.service.KanbanService;
import com.dsa.team5.kanban.service.StatusService;

@Controller
@RequestMapping("/")
public class HomeController {

	@Autowired
	KanbanService kanbanService;

	@Autowired
	StatusService statusService;
	
	@RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        return "hello";
    }

	@RequestMapping(value = "/redirect", method = RequestMethod.POST)
	public String showKanban(ModelMap model,
			@RequestParam("code") String kanbanCode) {
		System.out.println(kanbanCode);
		List<Status> statusList = new ArrayList<Status>();
		Kanban kanban = kanbanService.getKanbanByCode(kanbanCode);
		if (null == kanban) {
			kanban = new Kanban();
			kanban.setCode(kanbanCode);
			kanbanService.save(kanban);
			kanban = kanbanService.getKanbanByCode(kanbanCode);
			Status status = new Status();
			for (int i = 1; i <= 4; i++) {
				status = new Status();
				if (i == 1) {
					status.setKanban(kanban);
					status.setName("Todo");
					status.setPosition(i);
				} else if (i == 2) {
					status.setKanban(kanban);
					status.setName("In-progress");
					status.setPosition(i);
				} else if (i == 3) {
					status.setKanban(kanban);
					status.setName("Verify");
					status.setPosition(i);
				} else {
					status.setKanban(kanban);
					status.setName("Done");
					status.setPosition(i);
				}
				statusService.save(status);

			}
		}
		
		statusList = statusService.getStatusByKanbanId(kanban.getKanbanId());
		model.addAttribute("kanbanCode", kanbanCode);
		model.addAttribute("status", statusList);

		return "kanban";
	}
}